Pairs and Indexed-Pairs
Looping key-value pairs in a Lua table
I just wanted to discuss the function pairs() that we briefly saw in the previous lecture. The function pairs() returns a pair of key-value, so if we want to loop all the keys and values of a Lua table we can write:
As you can see, we can get the content of the key and also the value inside that key from our Lua table.
But sometimes, we have simpler Lua tables that use number indices instead of keys. If you want to get the index and the values per index, we can use the function ipairs().
Therefore, from now on, depending on what you need (key-value or index-value) you can pick the correct function… pairs() or ipairs().



